/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.cx1.dao.impl;

import com.ibatis.sqlmap.client.SqlMapClient;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.cx1.dao.api.IPlaceTypeDao;
import org.cx1.model.PlaceType;

/**
 *
 * @author tina
 */
public class PlaceTypeDao implements IPlaceTypeDao {
    
    private SqlMapClient sqlMapClient;

    public PlaceTypeDao() {
        sqlMapClient = IbatisConnectionFactory.getSqlMapClient();
    }
    
    @Override
    public String add(PlaceType placeType) {
        String placeTypeId = "";
        
        try {
            sqlMapClient.insert("placeType.add", placeType);
            placeTypeId = placeType.getId();
            
        } catch (SQLException ex) {
            Logger.getLogger(PlaceTypeDao.class.getName()).log(Level.SEVERE, null, ex);
        }
        
        return placeTypeId;
    }

    @Override
    public int update(PlaceType placeType) {
        int noOfRowsAffected = 0;
        
        try {
            noOfRowsAffected = sqlMapClient.update("placeType.update", placeType);
        } catch (SQLException ex) {
            Logger.getLogger(PlaceTypeDao.class.getName()).log(Level.SEVERE, null, ex);
        }
        
        return noOfRowsAffected;
    }

    @Override
    public PlaceType getById(String id) {
        PlaceType placeType = null;
        
        try {
            placeType = (PlaceType) sqlMapClient.queryForObject("placeType.getById", id);
        } catch (SQLException ex) {
            Logger.getLogger(PlaceTypeDao.class.getName()).log(Level.SEVERE, null, ex);
        }
        
        return placeType;
    }

    @Override
    public List<PlaceType> getAll() {
        List<PlaceType> placeTypes = new ArrayList<PlaceType>();
        
        try {
            placeTypes = sqlMapClient.queryForList("placeType.getAll");
        } catch (SQLException ex) {
            Logger.getLogger(PlaceTypeDao.class.getName()).log(Level.SEVERE, null, ex);
        }
        
        return placeTypes;
    }

    @Override
    public int deleteById(String id) {
        int noOfRowsAffected = 0;
        
        try {
            noOfRowsAffected = sqlMapClient.delete("placeType.deleteById", id);
        } catch (SQLException ex) {
            Logger.getLogger(PlaceTypeDao.class.getName()).log(Level.SEVERE, null, ex);
        }
        
        return noOfRowsAffected;
    }
}
